package com.whfc.mach.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.PageData;
import com.whfc.common.util.PageUtil;
import com.whfc.mach.dao.AppMachFileMapper;
import com.whfc.mach.dto.MachFileDTO;
import com.whfc.mach.entity.AppMachFile;
import com.whfc.mach.param.MachFileAddParam;
import com.whfc.mach.param.MachFileRenameParam;
import com.whfc.mach.service.AppMachFileService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author sun_guodong
 * @description
 * @date 2021-04-07
 */
@DubboService(interfaceClass = AppMachFileService.class, version = "1.0.0")
public class AppMachFileServiceImpl implements AppMachFileService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AppMachFileMapper machFileMapper;

    /**
     * 文件名最大长度
     */
    private static final int FILE_NAME_MAX_LENGTH = 50;

    @Override
    public PageData<MachFileDTO> list(Integer machId, Integer pageNum, Integer pageSize) throws BizException {
        logger.info("设备档案列表服务,machId:{},pageNum:{},pageSize:{}", machId, pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<MachFileDTO> list = machFileMapper.selectByMachId(machId);
        PageHelper.clearPage();
        return PageUtil.pageData(PageInfo.of(list));
    }

    @Override
    public void add(MachFileAddParam param) throws BizException {
        logger.info("添加设备档案服务,param:{}", param.toString());
        String name = param.getName();
        if (name.length() > FILE_NAME_MAX_LENGTH) {
            name = name.substring(name.length() - FILE_NAME_MAX_LENGTH);
        }
        AppMachFile record = new AppMachFile();
        BeanUtils.copyProperties(param, record);
        record.setName(name);
        machFileMapper.insertSelective(record);
    }

    @Override
    public void rename(MachFileRenameParam param) throws BizException {
        logger.info("设备档案重命名服务,param:{}", param.toString());
        machFileMapper.updateName(param.getFileId(), param.getName());
    }

    @Override
    public void del(Integer fileId) throws BizException {
        logger.info("删除设备档案服务,fileId:{}", fileId);
        machFileMapper.deleteLogic(fileId);
    }
}
